package com.huoyun.userAdmin.action;

import java.util.List;

import net.sf.json.JSONObject;

import com.huoyun.base.action.BaseAction;
import com.huoyun.base.service.RegionService;
import com.huoyun.userAdmin.bean.Address;
import com.huoyun.userAdmin.service.AddressService;

public class AddressAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5804536488244903125L;

	private AddressService addressService;
	
	private RegionService regionService;
	
	private Address address;
	
	private String jsonResult; 
	
	private long addressId;
	
	private List<Address> addressList;

	public String preAddAddress()
	{
		try
		{
			addressList = addressService.getAddressByUserId(getLoginUser().getId());
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		
		return SUCCESS;
	}
	
	public String ajaxPreAddAddress()
	{
		return SUCCESS;
	}
	
	public String addAddress()
	{
		try
		{
			addressList = addressService.getAddressByUserId(getLoginUser().getId());
			
			//如果用户的邮寄地址超过了10个，那么就不允许用户再添加
			if(addressList != null && addressList.size() >= 10)
			{
				return INPUT;
			}
			
			address.setUserId(getLoginUser().getId());
			address.setType(0);
			
			addressService.saveAddress(address);
			
			
		}catch (Exception e) {
			this.setResultMsg("添加邮寄地址失败");
			return INPUT;
		}
		return SUCCESS;
	}
	
	public String ajaxAddAddress(){
		try
		{
			address.setUserId(getLoginUser().getId());
			address.setType(0);
			
			addressService.saveAddress(address);
			
			//通过地址对应的区域code查询区域的名称，以方便前台展示
			address.setProvince(regionService.getRegionByCode(address.getProvince()).getName());
			address.setCity(regionService.getRegionByCode(address.getCity()).getName());
			address.setTown(regionService.getRegionByCode(address.getTown()).getName());
			
			jsonResult=JSONObject.fromObject(address).toString();

		}catch (Exception e) {
			this.setResultMsg("添加邮寄地址失败");
		}
		return SUCCESS;
	}
	
	public String ajaxDelAddress(){
		addressService.delete(addressId);
		
		return SUCCESS;
	}
	
	public String ajaxPreModifyAddress(){
		address=addressService.getAddressById(addressId);
		
		return SUCCESS;
	}
	
	public String ajaxModifyAddress(){
		//Address myAddress=addressService.getAddressById(addressId);
		addressService.update(address);
		
		//通过地址对应的区域code查询区域的名称，以方便前台展示
		address.setProvince(regionService.getRegionByCode(address.getProvince()).getName());
		address.setCity(regionService.getRegionByCode(address.getCity()).getName());
		address.setTown(regionService.getRegionByCode(address.getTown()).getName());
		
		JSONObject json=JSONObject.fromObject(address);
		jsonResult=json.toString();
		System.out.println(jsonResult);
		return SUCCESS;
	}
	
	public String preEditAddress()
	{
		try
		{
			address = addressService.getAddressById(addressId);
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		return SUCCESS;
	}
	
	public String editAddress()
	{
		return SUCCESS;
	}
	
	public AddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public List<Address> getAddressList() {
		return addressList;
	}

	public void setAddressList(List<Address> addressList) {
		this.addressList = addressList;
	}

	public long getAddressId() {
		return addressId;
	}

	public void setAddressId(long addressId) {
		this.addressId = addressId;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
	
	
}
